package com.icow.wms.basic.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.icow.wms.basic.domain.BarChart;
import com.icow.wms.basic.domain.LineChart;
import com.icow.wms.basic.domain.PieChart;
import com.icow.wms.basic.utils.OrderState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.icow.common.annotation.Log;
import com.icow.common.core.controller.BaseController;
import com.icow.common.core.domain.AjaxResult;
import com.icow.common.enums.BusinessType;
import com.icow.wms.basic.domain.AgvTask;
import com.icow.wms.basic.service.IAgvTaskService;
import com.icow.common.utils.poi.ExcelUtil;
import com.icow.common.core.page.TableDataInfo;

/**
 * taskController
 *
 * @date 2024-08-06
 */
@RestController
@RequestMapping({"/task/task", "/api/task"})
@Slf4j
public class AgvTaskController extends BaseController {
    @Autowired
    private IAgvTaskService agvTaskService;


    @PostMapping("/test01")
    public String test01(@RequestBody String json) {
        System.out.println(json);
        return "OK~~";
    }

    /**
     * 查询task列表
     */
    @PreAuthorize("@ss.hasPermi('task:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgvTask agvTask) {
        startPage();
        List<AgvTask> list = agvTaskService.selectAgvTaskList(agvTask);
        return getDataTable(list);
    }

    /**
     * 导出task列表
     */
    @PreAuthorize("@ss.hasPermi('task:task:export')")
    @Log(title = "task", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AgvTask agvTask) {
        List<AgvTask> list = agvTaskService.selectAgvTaskList(agvTask);
        ExcelUtil<AgvTask> util = new ExcelUtil<AgvTask>(AgvTask.class);
        return util.exportExcel(list, "task");
    }

    /**
     * 获取task详细信息
     */
    @PreAuthorize("@ss.hasPermi('task:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(agvTaskService.selectAgvTaskById(id));
    }

    /**
     * 新增task
     */
    @PreAuthorize("@ss.hasPermi('task:task:add')")
    @Log(title = "task", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgvTask agvTask) {
        return toAjax(agvTaskService.insertAgvTask(agvTask));
    }

    /**
     * 修改task
     */
    @PreAuthorize("@ss.hasPermi('task:task:edit')")
    @Log(title = "task", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgvTask agvTask) {
        return toAjax(agvTaskService.updateAgvTask(agvTask));
    }

    /**
     * 删除task
     */
    @PreAuthorize("@ss.hasPermi('task:task:remove')")
    @Log(title = "task", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(1);
    }


    @ResponseBody
    @GetMapping("/deleteFinishTask")
    public void deleteFinishTask() {
        agvTaskService.deleteThirtyDaysAgoData();
        log.info("Delete data from thirty days ago");
    }

    @ResponseBody
    @GetMapping("/listPieChart")
    public List<PieChart> listPieChart() {
        List<PieChart> pieCharts = Arrays.asList(agvTaskService.selectForPieChart(OrderState.ORDER_STATE_FAIL),
                agvTaskService.selectForPieChart(OrderState.ORDER_STATE_INIT),
                agvTaskService.selectForPieChart(OrderState.ORDER_STATE_EXECUTING),
                agvTaskService.selectForPieChart(OrderState.ORDER_STATE_LOAD_FINISH),
                agvTaskService.selectForPieChart(OrderState.ORDER_STATE_FINISH));
        return pieCharts;
    }


    @ResponseBody
    @GetMapping("/listLineChart")
    public List<LineChart> listLineChart() throws ParseException {
        List<LineChart> lineCharts = new ArrayList<>();
        for (int i = 29; i >= 0; i--) {
            lineCharts.add(agvTaskService.selectForLineChart(i));
        }
        return lineCharts;
    }


    @ResponseBody
    @GetMapping("/listBarChart")
    public List<BarChart> listBarChart() throws ParseException {
        List<BarChart> barCharts = new ArrayList<>();
        for (int i = 29; i >= 0; i--) {
            barCharts.add(agvTaskService.selectForBarChart(i));
        }
        return barCharts;
    }


}